package com.tianyu.service.mqtt.endpoint;

import com.tianyu.service.mqtt.common.MqttClientOptions;
import com.tianyu.service.mqtt.common.MqttMessageChannel;
import com.tianyu.service.mqtt.common.Third01MqttClientOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageHandler;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2023/3/9 - 14:42
 **********************************/


@Slf4j
@Component
public class MqttEndpoint {


    @Resource
    private MqttClientOptions mqttClientOptions;


    @Resource
    private Third01MqttClientOptions third01MqttClientOptions;

    @Resource
    private MqttPahoClientFactory mqttClientFactory;

    @Resource
    private MqttPahoClientFactory mqttThird01ClientFactory;

    @Bean(name = "mqttPahoMessageDrivenChannelAdapter")
    public MessageProducer mqttInbound() {
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
                mqttClientOptions.getClientId() + "-receive",
                mqttClientFactory, mqttClientOptions.getInboundTopic());
        DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
        converter.setPayloadAsBytes(true);
        adapter.setConverter(converter);
        adapter.setQos(mqttClientOptions.getSubQos());
        adapter.setOutputChannelName(MqttMessageChannel.ChannelName.INBOUND);
        return adapter;
    }

    @Bean
    @ServiceActivator(inputChannel = MqttMessageChannel.ChannelName.OUTBOUND)
    public MessageHandler mqttOutbound() {
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(
                mqttClientOptions.getClientId() + "-send",
                mqttClientFactory);
        DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
        converter.setPayloadAsBytes(true);
        messageHandler.setAsync(true);
        messageHandler.setDefaultQos(mqttClientOptions.getPubQos());
        messageHandler.setConverter(converter);
        return messageHandler;
    }

    @Bean
    @ServiceActivator(inputChannel = MqttMessageChannel.ChannelName.OUTBOUND_THIRD01)
    public MessageHandler mqttThird01Outbound() {
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(
                third01MqttClientOptions.getClientId(),
                mqttThird01ClientFactory);
        DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
        converter.setPayloadAsBytes(true);
        messageHandler.setAsync(true);
        messageHandler.setDefaultQos(third01MqttClientOptions.getPubQos());
        messageHandler.setConverter(converter);
        return messageHandler;
    }
}
